﻿#region Copyright information
// 
// Copyright © 2012-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the SubnetworkToolkit and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: GmtWriter.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@outlook.com
// - Date Created: 2013-01-30 2:55 PM
// - Last Modified: 2013-01-30 3:21 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using BOL.IO;

namespace SubnetworkToolkit.IO
{
    public class GmtWriter : WriterBase
    {
        private readonly bool _writeSymbol;
        private readonly IEnumerable<GeneSet> _geneSets;

        public GmtWriter(string path, IEnumerable<GeneSet> geneSets, bool writeSymbol = false)
            : base(path)
        {
            _geneSets = geneSets;
            _writeSymbol = writeSymbol;
        }

        public override bool Write()
        {
            try
            {
                foreach (var geneSet in _geneSets)
                {
                    StreamWriter.WriteLine("{0}\t{1}\t{2}", geneSet.Name, geneSet.Description,
                                           _writeSymbol
                                               ? string.Join("\t", geneSet.Genes.Select(x => String.Format("{0:s}", x)))
                                               : string.Join("\t", geneSet.Genes.OrderBy(x => x)));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }
    }
}